﻿#include "lvgl_desktop.h"

#define DESKTOP_ICON_MAX 11


#if 1
#define lvgl_desktop_debug(format, ...) lvgl_project_debug("[lvgl desktop]-",format,##__VA_ARGS__);
#else
#define lvgl_desktop_debug(format, ...) ;
#endif

__lvgl_desktop_data lvgl_desktop_data;

lv_obj_t** lvgl_desktop_image_buf[] = {

    &lvgl_desktop_data.image1 ,
    &lvgl_desktop_data.image2 ,
    &lvgl_desktop_data.image3 ,
    &lvgl_desktop_data.image4 ,
    &lvgl_desktop_data.image5 ,
    &lvgl_desktop_data.image6 ,
    &lvgl_desktop_data.image7 ,
    &lvgl_desktop_data.image8 ,
    &lvgl_desktop_data.image9 ,
    &lvgl_desktop_data.image10 ,
    &lvgl_desktop_data.image11 ,
};

const char* lvgl_desktop_img_DESKTOP_GIF_PATH_buf[] = {

    DESKTOP_GIF_PATH"img_clock.gif",
    DESKTOP_GIF_PATH"img_radio.gif",
    DESKTOP_GIF_PATH"img_music.gif",
    DESKTOP_GIF_PATH"img_weather.gif",
    DESKTOP_GIF_PATH"img_browser.gif",
    DESKTOP_GIF_PATH"img_tools.gif",
    DESKTOP_GIF_PATH"img_document.gif",
    DESKTOP_GIF_PATH"img_power.gif",
    DESKTOP_GIF_PATH"img_bilibili.gif",
};

const lv_img_dsc_t* lvgl_desktop_image_dsc_buf[] = {
#if DESKTOP_PNG_BG
    &img_clock,
    &img_radio,
    &img_temp,
    &img_bilibili,
    &img_weather,
    &img_laohuangli,
    &img_browser,
    &img_light,
    &img_tools,
    &img_document,
    &img_power
#else
    & img_clock_nobg,
    &img_radio_nobg,
    &img_music_nobg,
    &img_weather_nobg,
    &img_browser_nobg,
    &img_tools_nobg,
    &img_document_nobg,
    &img_power_nobg,

#endif

};


static void lvgl_desktop_anim_select_start(int targetpos);
static void lvgl_desktop_anim_out_start(int mode, int time);
static void lvgl_desktop_anim_select_ready_cb(lv_anim_t* anim);

void lvgl_desktop_task_cb(lv_task_t* t)
{


}
void lvgl_desktop_close_task_cb(lv_task_t* t)
{
    static uint8_t cnt = 0;
    if (lvgl_desktop_data.close_flag == true)
    {

        if (++cnt >= lvgl_desktop_data.close_count)
        {
            cnt = 0;
            lvgl_desktop_data.close_flag = false;
            lvgl_task_delete(&lvgl_desktop_data.close_task);


            if (lvgl_desktop_data.close_num == 0)
            {

                lvgl_desktop_anim_out_start(0, 0);
            }
            else
            if (lvgl_desktop_data.close_num == 1)
            {
                lvgl_desktop_anim_out_start(0, 0);
            }
            else
            if (lvgl_desktop_data.close_num == 2)
            {
                lvgl_desktop_anim_out_start(0, 0);
            }
            else
            if (lvgl_desktop_data.close_num == 3)
            {
                lvgl_desktop_anim_out_start(0, 0);
            }
            else
            if (lvgl_desktop_data.close_num == 4)
            {
                lvgl_desktop_anim_out_start(0, 0);
            }
            else
            if (lvgl_desktop_data.close_num == 5)
            {
                lvgl_desktop_anim_out_start(0, 0);
            }
            else
            if (lvgl_desktop_data.close_num == 6)
            {
                lvgl_hint_create(lv_scr_act(), lvgl_globa_text[0][system_get_Language()], 200, 3);
            }
            else
            if (lvgl_desktop_data.close_num == 7)
            {
                lvgl_backlight_create(lv_scr_act());
                //lvgl_hint_create(lv_scr_act(), lvgl_globa_text[0][system_get_Language()], 200, 3);
            }
            else
            if (lvgl_desktop_data.close_num == 8)
            {

                lvgl_hint_create(lv_scr_act(), lvgl_globa_text[0][system_get_Language()], 200, 3);
            }
            else
            if (lvgl_desktop_data.close_num == 9)
            {
                lvgl_hint_create(lv_scr_act(), lvgl_globa_text[0][system_get_Language()], 200, 3);
            }
            else
            if (lvgl_desktop_data.close_num == 10)
            {
                if (0)//system_get_power_on_type() == BSP_POWER_ON_TYPE_USB)
                {
                    lvgl_hint_create(lv_scr_act(), lvgl_globa_text[24][system_get_Language()], 200, 10);
                }
                else
                {

                    lvgl_power_off_create(lv_scr_act());
                    //lvgl_desktop_anim_out_start(0, 0);
                    //lvgl_hint_create(lv_scr_act(), lvgl_globa_text[25][system_get_Language()], 200, 10);
                }

            }
        }

    }

}


void lvgl_desktop_icon_select(uint8_t num)
{


    lvgl_desktop_data.select_anim_ready = false;

    lvgl_desktop_debug("select:%d x:%d", num, 96 - num * 160);

    lvgl_desktop_anim_select_start(96 - num * 160);


}


static void lvgl_desktop_anim_select_cb(void* arc, lv_anim_value_t v)
{
    lv_obj_set_x(arc, v);
}

static void lvgl_desktop_anim_select_ready_cb(lv_anim_t* anim)
{

    lvgl_desktop_debug("---------anim select ready---------");
    lv_anim_del(&lvgl_desktop_data.anim_select, NULL);
    lvgl_desktop_data.select_anim_ready = true;

}

static void lvgl_desktop_anim_select_start(int targetpos)
{
#if LVGL_ANIM_SWITCH

    lvgl_desktop_debug("---------anim select start---------");

    lv_anim_path_init(&lvgl_desktop_data.anim_path_select);
    lv_anim_path_set_cb(&lvgl_desktop_data.anim_path_select, lv_anim_path_ease_out);
    //lv_anim_path_linear线性动画
    //lv_anim_path_step最后一步更改
    //lv_anim_path_ease_in开头很慢
    //lv_anim_path_ease_out最后慢
    //lv_anim_path_ease_in_out在开始和结束时也很慢
    //lv_anim_path_overshoot超出最终值
    //lv_anim_path_bounce从最终值反弹一点（就像撞墙一样）


    lv_anim_init(&lvgl_desktop_data.anim_select);


    lv_anim_set_exec_cb(&lvgl_desktop_data.anim_select, lvgl_desktop_anim_select_cb);
    lv_anim_set_ready_cb(&lvgl_desktop_data.anim_select, lvgl_desktop_anim_select_ready_cb);
    lv_anim_set_values(&lvgl_desktop_data.anim_select, lv_obj_get_x(lvgl_desktop_data.cont_select), targetpos);
    lv_anim_set_time(&lvgl_desktop_data.anim_select, 400);
    lv_anim_set_var(&lvgl_desktop_data.anim_select, lvgl_desktop_data.cont_select);

    lv_anim_set_path(&lvgl_desktop_data.anim_select, &lvgl_desktop_data.anim_path_select);
    lv_anim_start(&lvgl_desktop_data.anim_select);

#else


    //lvgl_desktop_anim_in_ready_cb(NULL);
    lv_obj_set_x(lvgl_desktop_data.cont_main, targetpos);
    lvgl_desktop_data.select_anim_ready = true;

#endif




}

void lvgl_desktop_anim_in_cb(void* arc, lv_anim_value_t v)
{
    lv_obj_set_y(arc, v);
}

void lvgl_desktop_anim_in_ready_cb(lv_anim_t* anim)
{

    lvgl_desktop_debug("---------anim in ready---------");

    lv_anim_del(&lvgl_desktop_data.anim_in, NULL);

    lv_obj_set_pos(lvgl_desktop_data.cont_main, 0, 0);

    lvgl_group_add_obj(&lvgl_desktop_data.cont_main);
    lvgl_group_focus_obj(&lvgl_desktop_data.cont_main);

}

void lvgl_desktop_anim_in_start(int mode, int time)
{
#if LVGL_ANIM_SWITCH

    lvgl_desktop_debug("---------anim in start---------");

    lv_anim_path_init(&lvgl_desktop_data.anim_path_in);
    lv_anim_path_set_cb(&lvgl_desktop_data.anim_path_in, LVGL_ANIM_IN_PATH);

    //lv_anim_path_linear线性动画
    //lv_anim_path_step最后一步更改
    //lv_anim_path_ease_in开头很慢
    //lv_anim_path_ease_out最后慢
    //lv_anim_path_ease_in_out在开始和结束时也很慢
    //lv_anim_path_overshoot超出最终值
    //lv_anim_path_bounce从最终值反弹一点（就像撞墙一样）

    lv_anim_init(&lvgl_desktop_data.anim_in);

    lv_anim_set_exec_cb(&lvgl_desktop_data.anim_in, lvgl_desktop_anim_in_cb);
    lv_anim_set_ready_cb(&lvgl_desktop_data.anim_in, lvgl_desktop_anim_in_ready_cb);
    lv_anim_set_values(&lvgl_desktop_data.anim_in, lv_obj_get_y(lvgl_desktop_data.cont_main), 0);
    lv_anim_set_time(&lvgl_desktop_data.anim_in, LVGL_ANIM_IN_TIME);
    lv_anim_set_var(&lvgl_desktop_data.anim_in, lvgl_desktop_data.cont_main);

    lv_anim_set_path(&lvgl_desktop_data.anim_in, &lvgl_desktop_data.anim_path_in);
    lv_anim_start(&lvgl_desktop_data.anim_in);

#else


    lvgl_desktop_anim_in_ready_cb(NULL);

#endif




}


void lvgl_desktop_anim_out_cb(void* arc, lv_anim_value_t v)
{
    lv_obj_set_y(arc, v);
}
void lvgl_desktop_anim_out_ready_cb(lv_anim_t* anim)
{

    lvgl_desktop_debug("---------anim out ready---------");
    lv_anim_del(&lvgl_desktop_data.anim_out, NULL);

    lvgl_desktop_close();

}

static void lvgl_desktop_anim_out_start(int mode, int time)
{

    lvgl_group_remove_obj(&lvgl_desktop_data.cont_main);

#if LVGL_ANIM_SWITCH

    lvgl_desktop_debug("---------anim out start---------");

    lv_anim_path_init(&lvgl_desktop_data.anim_path_out);
    lv_anim_path_set_cb(&lvgl_desktop_data.anim_path_out, LVGL_ANIM_OUT_PATH);
    //lv_anim_path_linear线性动画
    //lv_anim_path_step最后一步更改
    //lv_anim_path_ease_in开头很慢
    //lv_anim_path_ease_out最后慢
    //lv_anim_path_ease_in_out在开始和结束时也很慢
    //lv_anim_path_overshoot超出最终值
    //lv_anim_path_bounce从最终值反弹一点（就像撞墙一样）



    lv_anim_init(&lvgl_desktop_data.anim_out);


    lv_anim_set_exec_cb(&lvgl_desktop_data.anim_out, lvgl_desktop_anim_out_cb);
    lv_anim_set_ready_cb(&lvgl_desktop_data.anim_out, lvgl_desktop_anim_out_ready_cb);
    lv_anim_set_values(&lvgl_desktop_data.anim_out, lv_obj_get_y(lvgl_desktop_data.cont_main), lv_obj_get_height(lv_scr_act()));
    lv_anim_set_time(&lvgl_desktop_data.anim_out, LVGL_ANIM_OUT_TIME);
    lv_anim_set_var(&lvgl_desktop_data.anim_out, lvgl_desktop_data.cont_main);

    lv_anim_set_path(&lvgl_desktop_data.anim_out, &lvgl_desktop_data.anim_path_out);
    lv_anim_start(&lvgl_desktop_data.anim_out);

#else


    lvgl_desktop_anim_out_ready_cb(NULL);

#endif

}


void lvgl_desktop_event_cb(struct _lv_obj_t* obj, lv_event_t event)
{
    if (obj == lvgl_desktop_data.cont_select)
    {
        switch (event)
        {
        case LV_EVENT_DRAG_END://拖动结束后

            if (lv_obj_get_x(lvgl_desktop_data.cont_select) != 0)
            {
                lvgl_desktop_icon_select(lvgl_desktop_data.select_icon);
            }
            break;
        }
    }
    else
        if (obj == lvgl_desktop_data.cont_main)
        {
            if (lvgl_desktop_data.select_anim_ready == false)
            {
                //return;
            }
            //lvgl_desktop_debug("cb:%d", event);
            switch (event)
            {
            case LV_EVENT_KEY:

                switch (*(const uint32_t*)lv_event_get_data())
                {
                case LV_KEY_UP:
                    lvgl_desktop_debug("LV_KEY_UP");

                    break;
                case LV_KEY_DOWN:
                    lvgl_desktop_debug("LV_KEY_DOWN");

                    break;
                case LV_KEY_LEFT:
                    lvgl_desktop_debug("LV_KEY_LEFT");

                    if (--lvgl_desktop_data.select_icon < 0)
                    {
                        lvgl_desktop_data.select_icon = DESKTOP_ICON_MAX - 1;
                    }
                    lvgl_desktop_icon_select(lvgl_desktop_data.select_icon);

                    break;
                case LV_KEY_RIGHT:
                    lvgl_desktop_debug("LV_KEY_RIGHT");
                    if (++lvgl_desktop_data.select_icon >= DESKTOP_ICON_MAX)
                    {
                        lvgl_desktop_data.select_icon = 0;
                    }
                    lvgl_desktop_icon_select(lvgl_desktop_data.select_icon);
                    break;
                case LV_KEY_ESC:
                    lvgl_desktop_debug("LV_KEY_ESC");

                    //lvgl_desktop_anim_out_start(0, 0);
                    break;
                case LV_KEY_DEL:
                    lvgl_desktop_debug("LV_KEY_DEL");

                    break;
                case LV_KEY_BACKSPACE:
                    lvgl_desktop_debug("LV_KEY_BACKSPACE");

                    break;
                case LV_KEY_ENTER:
                    lvgl_desktop_debug("LV_KEY_ENTER");

                    lvgl_desktop_data.close_flag = true;
                    lvgl_desktop_data.close_count = 1;
                    lvgl_desktop_data.close_num = lvgl_desktop_data.select_icon;
                    lvgl_task_create(&lvgl_desktop_data.close_task, lvgl_desktop_close_task_cb, 10, LV_TASK_PRIO_HIGH, NULL);



                    break;
                case LV_KEY_NEXT:
                    lvgl_desktop_debug("LV_KEY_NEXT");

                    break;
                case LV_KEY_PREV:
                    lvgl_desktop_debug("LV_KEY_PREV");

                    break;
                case LV_KEY_HOME:
                    lvgl_desktop_debug("LV_KEY_HOME");

                    break;
                case LV_KEY_END:
                    lvgl_desktop_debug("LV_KEY_END");

                    break;


                }

                break;



            case LV_EVENT_DELETE:

                lvgl_desktop_data.cont_main = NULL;
                lvgl_desktop_debug("delete win");
                break;

            case LV_EVENT_PRESSING://对象被持续按下

                break;
            case LV_EVENT_PRESSED://对象已被按下

                break;
            case LV_EVENT_DRAG_END://拖动结束后

                if (lv_obj_get_x(lvgl_desktop_data.cont_main) != 0)
                {
                    lv_obj_set_x(lvgl_desktop_data.cont_main, 0);
                }

                if (lv_obj_get_y(lvgl_desktop_data.cont_main) != 0)
                {
                    lv_obj_set_y(lvgl_desktop_data.cont_main, 0);
                }
                break;
            case LV_EVENT_RELEASED://按钮释放

            case LV_EVENT_LONG_PRESSED://按钮长按

                break;

            default:
                break;
            }
        }
}


void lvgl_desktop_create(lv_obj_t* parent)
{
    if (lvgl_desktop_data.cont_main == NULL)
    {

        //----创建容器----//
        lvgl_desktop_data.cont_main = lv_cont_create(parent, NULL);

        lv_obj_set_size(lvgl_desktop_data.cont_main, lv_obj_get_width(parent), lv_obj_get_height(parent));
        lv_obj_set_pos(lvgl_desktop_data.cont_main, 0, -(lv_obj_get_height(parent)));

        lv_obj_set_click(lvgl_desktop_data.cont_main, true); //启用 / 禁用可点击
        lv_obj_set_drag(lvgl_desktop_data.cont_main, true);//启用/禁用对象可拖动
        lv_obj_set_drag_throw(lvgl_desktop_data.cont_main, true);//启用/禁用拖动后是否有惯性移动
        lv_obj_set_drag_parent(lvgl_desktop_data.cont_main, false); //启用 / 禁用父对象可拖动
        lv_obj_set_drag_dir(lvgl_desktop_data.cont_main, LV_DRAG_DIR_HOR);//设置拖动方向
        lv_obj_add_style(lvgl_desktop_data.cont_main, LV_OBJ_PART_MAIN, &lvgl_style.cont_no_border);
        lv_obj_set_event_cb(lvgl_desktop_data.cont_main, lvgl_desktop_event_cb);
        //----创建容器----//


        lvgl_desktop_data.cont_select = lv_cont_create(lvgl_desktop_data.cont_main, NULL);
        lv_obj_set_size(lvgl_desktop_data.cont_select, DESKTOP_ICON_MAX * (128 + 32), lv_obj_get_height(parent));
        lv_obj_add_style(lvgl_desktop_data.cont_select, LV_OBJ_PART_MAIN, &lvgl_style.cont_transparent);

        for (int i = 0; i < DESKTOP_ICON_MAX; i++)
        {
#if DESKTOP_GIF_SW
            * lvgl_desktop_image_buf[i] = lv_gif_create_from_file(lvgl_desktop_data.cont_select, lvgl_desktop_img_DESKTOP_GIF_PATH_buf[i]);
            lvgl_desktop_debug("-%d:%s-", i, lvgl_desktop_img_DESKTOP_GIF_PATH_buf[i]);
            lv_obj_set_pos(*lvgl_desktop_image_buf[i], 0 + i * (32 + 128), 22);
#else
            * lvgl_desktop_image_buf[i] = lv_img_create(lvgl_desktop_data.cont_select, NULL);
            lv_img_set_src(*lvgl_desktop_image_buf[i], lvgl_desktop_image_dsc_buf[i]);
# if DESKTOP_PNG_BG
            lv_obj_set_pos(*lvgl_desktop_image_buf[i], 0 + i * (32 + 128), 22);
#else
            lv_obj_set_pos(*lvgl_desktop_image_buf[i], 0 + i * (32 + 128), 42);
#endif
#endif

        }
        lvgl_desktop_icon_select(lvgl_desktop_data.select_icon);

        lvgl_task_create(&lvgl_desktop_data.task, lvgl_desktop_task_cb, 200, LV_TASK_PRIO_HIGH, NULL);

        lvgl_desktop_debug("create win");
    }
    else
    {
        lvgl_desktop_debug("show win");
    }

    lv_obj_move_background(lvgl_desktop_data.cont_main);
    lvgl_set_obj_show(&lvgl_desktop_data.cont_main);

    lvgl_desktop_anim_in_start(0, 0);
}

void lvgl_desktop_hidden(void)
{

}

void lvgl_desktop_close(void)
{
    lvgl_desktop_debug("close");
    lvgl_group_remove_obj(&lvgl_desktop_data.cont_main);

    lvgl_task_delete(&lvgl_desktop_data.close_task);
    lvgl_task_delete(&lvgl_desktop_data.task);

    lvgl_obj_clean(&lvgl_desktop_data.cont_main);
    lvgl_obj_del(&lvgl_desktop_data.cont_main);


    switch (lvgl_desktop_data.close_num)
    {
    case 0:

        switch (system_get_clock_style())
        {
        case 0:
            lvgl_clock_create(lv_scr_act());
            break;
        case 1:
            lvgl_clock_style2_create(lv_scr_act());
            break;
        }

        break;
    case 1:
        lvgl_radio_create(lv_scr_act());
        break;
    case 2:
        lvgl_temp_create(lv_scr_act());
        break;
    case 3:
        lvgl_bilibili_create(lv_scr_act());
        break;
    case 4:
        lvgl_weather_create(lv_scr_act());
        break;
    case 5:
        lvgl_laohuangli_create(lv_scr_act());
        break;

    case 6:

        break;
    case 7:

        break;
    case 8:

        break;
    case 9:

        break;
    case 10:

        break;

    default:
        lvgl_clock_create(lv_scr_act());
        break;
    }

}










